package com.wenson.behavior.command;

/**
 * @ClassName: FlashCommand
 * @Author: luowangcheng
 * @Date: 2024/6/16 12:50
 * @Description:
 */
public class FlashCommand implements Command {
    private Bulb bulb;
    private volatile boolean neonRun = false;

    public FlashCommand(Bulb bulb) {
        this.bulb = bulb;
    }

    @Override
    public void exe() {
        if (!neonRun) {
            neonRun = true;
            System.out.println("霓虹灯闪烁任务启动");
            new Thread(() -> {
                try {
                    while (neonRun) {
                        bulb.on();
                        Thread.sleep(500);
                        bulb.off();
                        Thread.sleep(500);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    @Override
    public void unexe() {
        neonRun = false;
        System.out.println("霓虹灯闪烁任务结束");
    }
}
